package com.sunhao.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sunhao.myapplication.utils.DateUtil;

public class MainActivity8 extends AppCompatActivity implements View.OnClickListener {


    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main8);
        //获取结果
        tv_result = findViewById(R.id.tv_result);
        //获取单独点击按钮
        TextView partClick = findViewById(R.id.partClick);
//        partClick.setOnClickListener(new MyOnClick( tv_result));
//        partClick.setOnLongClickListener(view -> {
//            tv_result.setText(String.format("%s 您点击了按钮 %s", DateUtil.getNowTime(), ((Button) view).getText()));
//            return true;
//        });
        partClick.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                tv_result.setText(String.format("%s 您点击了按钮 %s", DateUtil.getNowTime(), ((Button) view).getText()));
                return true;
            }
        });


        View publicClick = findViewById(R.id.publicClick);
        publicClick.setOnClickListener(this);

    }
    static class MyOnClick implements View.OnClickListener {
        private TextView tv_result;
        public MyOnClick(TextView tv_result) {
            this.tv_result = tv_result;
        }
        @Override
        public void onClick(View view) {
            tv_result.setText(String.format("%s 您点击了按钮 %s", DateUtil.getNowTime(), ((Button) view).getText()));
        }

    }


    //公共点击事件
    @Override
    public void onClick(View view) {
        if(R.id.publicClick==view.getId()){
            tv_result.setText(String.format("%s 您点击了按钮 %s", DateUtil.getNowTime(), ((Button) view).getText()));
        }
    }
}